package com.huang825172.sbog.MainPage.View;

import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TextView;

import com.huang825172.sbog.R;
import com.huang825172.sbog.Tabs.Communicate.ChatListPage.View.ChatListPage;
import com.huang825172.sbog.Tabs.Mine.MinePage.View.MinePage;
import com.huang825172.sbog.Tabs.SoundAndExplore.VideoPage.View.VideoPage;
import com.huang825172.sbog.Util.StatusBarStatus;
import com.huang825172.sbog.Util.SystemUIControler;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements VideoPage.OnFragmentInteractionListener, ChatListPage.OnFragmentInteractionListener, MinePage.OnFragmentInteractionListener {
    @BindView(R.id.mainPageViewPager)
    ViewPager viewPager;
    @BindView(R.id.mainPageTabLayout)
    TabLayout tabLayout;

    @BindArray(R.array.titles)
    String[] titles;

    private List<Drawable> icons = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private MainPageFragmentAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        icons.add(getDrawable(R.mipmap.main_home));
        icons.add(getDrawable(R.mipmap.mian_shop));
        icons.add(getDrawable(R.mipmap.main_message));
        icons.add(getDrawable(R.mipmap.main_me));
        fragments.add(VideoPage.newInstance(getString(R.string.videoPageTypeMusic)));
        fragments.add(VideoPage.newInstance(getString(R.string.videoPageTypeBuy)));
        fragments.add(new ChatListPage());
        fragments.add(new MinePage());
        adapter = new MainPageFragmentAdapter(getSupportFragmentManager(), titles, fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setSelectedTabIndicator(getDrawable(R.mipmap.main_indicator));
        setUpCustomTab();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    private void setUpCustomTab() {
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            tabLayout.getTabAt(i).setCustomView(getTabView(i, false));
        }
    }

    private View getTabView(int i, boolean selected) {
        View v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.custom_tab, null);
        TextView title = (TextView) v.findViewById(R.id.customTabTitle);
        title.setText(titles[i]);
        ImageView icon = (ImageView) v.findViewById(R.id.customTabIcon);
        icon.setImageDrawable(icons.get(i));
        if (selected) {
            title.setTextColor(getResources().getColor(R.color.purple));
        } else {
            title.setTextColor(getResources().getColor(R.color.lighterGray));
        }
        return v;
    }
}
